﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DingHeng.TransportationManagement.WebSite.userControls
{
    [DefaultEvent("PageSelected")]
    public partial class CutPageControl : System.Web.UI.UserControl
    {
        #region Fields
        /// <summary>
        /// 分页跳转事件
        /// </summary>
        public event EventHandler PageSelected;
        private int m_index = 0;
        private int m_count = 0;
        private int m_pageSize = 0;
        private int m_dataCount = 0;
        #endregion

        #region 属性
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get
            {
                if (EnableViewState)
                {
                    this.m_index = Convert.ToInt32(ViewState[this.ClientID + "_index"] ?? 0);
                }

                if (this.m_index < 0)
                    this.m_index = 0;
                return this.m_index;
            }
            set
            {
                if (value >= 0)
                {
                    this.m_index = value;
                    if (EnableViewState)
                    {
                        ViewState[this.ClientID + "_index"] = value;
                    }
                }
            }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int DataCount
        {
            get
            {
                if (EnableViewState)
                {
                    this.m_dataCount = Convert.ToInt32(ViewState[this.ClientID + "_dataCount"] ?? 0);
                }

                if (this.m_dataCount < 0)
                    this.m_dataCount = 0;
                return this.m_dataCount;
            }
            set
            {
                if (value >= 0)
                {
                    this.m_dataCount = value;
                    if (EnableViewState)
                    {
                        ViewState[this.ClientID + "_dataCount"] = value;
                    }
                }
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (EnableViewState)
                {
                    this.m_count = Convert.ToInt32(ViewState[this.ClientID + "_count"] ?? 0);
                }

                if (this.m_count < 0)
                    this.m_count = 0;
                return this.m_count;
            }
            private set
            {
                if (value >= 0)
                {
                    this.m_count = value;
                    if (EnableViewState)
                    {
                        ViewState[this.ClientID + "_count"] = value;
                    }
                }
            }
        }
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                if (EnableViewState)
                {
                    this.m_pageSize = Convert.ToInt32(ViewState[this.ClientID + "_pageSize"] ?? 0);
                }

                if (this.m_pageSize < 1)
                    this.m_pageSize = 1;
                return this.m_pageSize;
            }
            set
            {
                if (value >= 0)
                {
                    this.m_pageSize = value;
                    if (EnableViewState)
                    {
                        ViewState[this.ClientID + "_pageSize"] = value;
                    }
                }
            }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 绑定
        /// </summary>
        public void PageBind()
        {
            bool first = false, prev = false, next = false, last = false, go = false;//默认禁用

            if (this.m_dataCount < 1)
            {
                this.PageCount = 0;
                this.PageIndex = 0;
            }
            else
            {
                if (this.PageIndex < 1)//有数据时 默认为第一页
                    this.PageIndex = 1;

                this.PageCount = this.m_dataCount / this.m_pageSize + (this.m_dataCount % this.m_pageSize == 0 ? 0 : 1);

                if (this.PageIndex == 1 && this.PageCount > 1)//首页时
                {
                    next = true;
                    last = true;
                }
                else if (this.PageIndex > 1 && this.PageIndex < this.PageCount)//中间页时
                {
                    first = true;
                    prev = true;
                    next = true;
                    last = true;
                }
                else if (this.PageIndex == this.PageCount && this.PageCount > 1)//尾页时
                {
                    first = true;
                    prev = true;
                }
                go = true;
            }


            #region 翻页设置


            this.SetLinkButton(lbtnFirst, first, 1);
            this.SetLinkButton(lbtnPrev, prev, this.PageIndex - 1);
            this.SetLinkButton(lbtnNext, next, this.PageIndex + 1);
            this.SetLinkButton(lbtnLast, last, this.PageCount);

            btnGo.Enabled = go;

            if (btnGo.Enabled)
            {
                //没有输入翻页数字时 不进行翻页 
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CheckGoPage", string.Format(@"<script type=""text/javascript"">function CheckGoPage(){{var num = parseInt($.trim($('#{0}').val()));if(isNaN(num) || num<{1} || num>{2}){{alert('{3}'); return false;}}else{{ return true;}}}}</script>", tbPage.ClientID, 1, this.PageCount, string.Format("请输入{0} - {1}之间的数字!", 1, this.PageCount)));
                btnGo.OnClientClick = "return CheckGoPage()";//string.Format(@"function CheckGoPage(){var num = parseInt($.trim($('#{0}').val()));if(num==NaN || num<{1} || num>{2}) return false;}", tbPage.ClientID, this.PageIndex, this.PageCount);//,GetGlobalResourceObject("CutPage","GoMessage")
            }
            //if (btnGo.Enabled)
            //{
            //    //没有输入翻页数字时 不进行翻页 
            //    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CheckGoPage", string.Format(@"<script type=""text/javascript"">function CheckGoPage(){{var num = parseInt($.trim($('#{0}').val()));if(isNaN(num) || num<{1} || num>{2}){{alert('{3}'); return false;}}else{{ return true;}}}}</script>", tbPage.ClientID, 1, this.PageCount, string.Format(GetGlobalResourceObject("CutPage", "PleaseInput").ToString(), 1, this.PageCount)));
            //    btnGo.OnClientClick = "return CheckGoPage()";
            //    //string.Format(@"function CheckGoPage(){var num = parseInt($.trim($('#{0}').val()));if(num==NaN || num<{1} || num>{2}) return false;}", tbPage.ClientID, this.PageIndex, this.PageCount);//,GetGlobalResourceObject("CutPage","GoMessage")
            //}

            #endregion


        }

        private void SetLinkButton(LinkButton lb, bool isEnabled, int pageIndex)
        {
            string str = lb.ID.Substring(4);
            lb.Enabled = isEnabled;
            if (lb.Enabled)
            {
                lb.CssClass = string.Format("pagebt page{0}", str);
                lb.CommandArgument = pageIndex.ToString();
                // lb.Command += new CommandEventHandler(PageSelected_Command);
            }
            else
            {
                lb.CssClass = string.Format("pagebt page{0}_gray", str);
            }
        }
        #endregion

        #region M
        protected void PageSelected_Command(object sender, CommandEventArgs e)
        {
            this.PageIndex = Convert.ToInt32(e.CommandArgument);
            if (PageSelected != null)
            {
                PageSelected(sender, e);
            }
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            int p = 1;
            int.TryParse(tbPage.Text, out p);
            this.PageIndex = p;
            if (PageSelected != null)
            {
                PageSelected(sender, e);
            }
            tbPage.Focus();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
        }
        #endregion
    }
}